package com.example.views;

import com.example.interfaces.IMainActivity;
import com.example.presenters.MainPresenter;
import com.example.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

/**
 * This is the main login page. It will take input from the LoginScreen and take
 * the most appropriate action. If a user exists, it will log them in. If they
 * do not exist, or if they ask for it, it will help create an account for them
 * by transitioning to the CreateAccountScreen and CreateAccountController.
 * 
 * @author 3 Factorial
 */
public class MainActivity extends Activity implements IMainActivity {
	private MainPresenter myPresenter;
	public final static String USERNAME = "com.example.first.USERNAME";
	public final static String ITEMNAME = "com.example.first.ITEMNAME";

	/**
	 * Activates what MainActivity will do.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		myPresenter = new MainPresenter(this,
				com.example.model.UserList.getInstance());
	}

	/**
	 * Inflates the menu options.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * Communicates with the user database. If there is a match with the fields
	 * and a user, logs the user in. Otherwise, prints an error message.
	 */
	public void login(View view) {

		EditText username = (EditText) findViewById(R.id.username_edit);
		EditText password = (EditText) findViewById(R.id.password_edit);
		String sUsername = username.getText().toString();
		String sPassword = password.getText().toString();
		myPresenter.login(sUsername, sPassword);
	}

	/**
	 * Switches to the RegisterActivity.
	 * 
	 * @param view
	 */
	public void createAccount(View view) {
		Intent intent = new Intent(this, RegisterActivity.class);
		startActivity(intent);
	}

	/**
	 * Switches to the ProfileActivity.
	 * 
	 * @param userName
	 */
	public void goToProfile(String userName) {
		Intent intent = new Intent(this, ProfileActivity.class);
		intent.putExtra(USERNAME, userName);
		startActivity(intent);
	}

	/**
	 * Displays an error text when login fails.
	 */
	public void dispNoUserFound() {
		TextView tv = (TextView) findViewById(R.id.login_error_text);
		tv.setText("User not found");
	}

	/**
	 * Displays a message saying the user is locker out.
	 */
	public void dispLockedOut(String userName) {
		TextView tv = (TextView) findViewById(R.id.login_error_text);
		tv.setText("Your account (" + userName
				+ ") has been locked. Please contact an administrator.");
	}

	/**
	 * Displays a message saying the password provided is wrong.
	 */
	public void dispWrongPassword() {
		TextView tv = (TextView) findViewById(R.id.login_error_text);
		tv.setText("Wrong password, try again.");
	}

} // End of MainPresenter method.
